package array;

import java.util.ArrayList;
import java.util.List;

public class leetCode54_spiralOrder {

    public static void main(String[] args) {
        int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        List<Integer> integers = spiralOrder(matrix);
        System.out.println(0);
    }
    public static List<Integer> spiralOrder(int[][] matrix) {
       List<Integer> result = new ArrayList<>();
       if(matrix == null || matrix.length == 0) return result;
       int n = matrix.length;
       int m = matrix[0].length;
       int left = 0;
       int right = m - 1;
       int top = 0;
       int bottom = n - 1;
        int total = n * m;
       while (left <= right && top <= bottom && result.size() < total){
           for (int i = left; i <= right  && result.size() < total; i++) {
               result.add(matrix[top][i]);
           }
           top++;
           for (int i = top; i <= bottom  && result.size() < total; i++) {
               result.add(matrix[i][right]);
           }
           right--;

           for (int i = right; i >= left  && result.size() < total; i--) {
               result.add(matrix[bottom][i]);
           }
           bottom--;

           for (int i = bottom; i >= top  && result.size() < total; i--) {
               result.add(matrix[i][left]);
           }
           left++;
       }

        return result;
    }


}
